/*
 * Copyright (c) 2006, 2023, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>Project: jse2302 - ListDemo
 * <p>Powered by webrx On 2023-06-25 09:43:21
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class ListDemo {

    public static void main(String[] args) {
        //1 List<Integer> list = new ArrayList<Integer>();
        //2 List<Integer> list = new ArrayList<>();
        //var list = new ArrayList<Double>();
        //var list = new ArrayList();

        //实例化集合List并使用泛型
        List<Integer> list1 = new ArrayList<>();
        //ArrayList<Integer> list2 = new ArrayList<>();

        //添加元素
        //System.out.println(list1.size());
        list1.add(1);
        list1.add(0, 30);
        list1.add(0, 35);

        //修改值
        list1.set(0, 45);
        //list1.add(1000,5);
        //System.out.println(list1.size());
        //System.out.println();

        list1.addAll(List.of(10, 20, 30, 40));
        list1.addAll(2, List.of(11, 22, 33, 44));

        System.out.println(list1);

        //判断
        System.out.println(list1.contains(33));
        System.out.println(list1.containsAll(List.of(10, 30)));
        System.out.println(list1.containsAll(List.of(10, 100)));
        System.out.println(list1.indexOf(30));
        System.out.println(list1.lastIndexOf(30));
        System.out.println(list1.lastIndexOf(3000));

        System.out.println("-----删除");
        //删除清空
        //Integer obj = 30;
        //list1.remove(obj);
        //list1.remove(0);
        //list1.removeAll(List.of(30)); //删除所有30元素
        //list1.removeIf(e -> e % 2 == 0); //删除所有偶数
        //list1.removeIf(a -> a == 30);//删除所有30

        //list1.clear();

        //System.out.println(list1.size());
        //System.out.println(list1);

        //获取元素，获取元素个数，遍历集合
        //System.out.println(list1.get(0));
        //System.out.println(list1.get(1));
        //System.out.println(list1.get(2));


        //indexOf();

        System.out.println("hello".indexOf("aa"));//-1
        System.out.println("hello".indexOf("h"));//0

        System.out.println(list1.get(6));
        for (Integer i : list1) {
            System.out.println(i);
            //System.out.printf("list1[%d] = %d%n", list1.indexOf(i), i);
        }

        for (int i = 0; i < list1.size(); i++) {
            System.out.printf("list1[%d] = %d%n", i, list1.get(i));
        }

        //list1.forEach(System.out::println);


    }


    public static void demo(String[] args) {
        //Object[] 相当
        List list = new ArrayList();
        list.add(1);
        list.add(.2);
        list.add(3L);
        list.add(5f);
        list.add("hello");
        list.add(new Date());

        //获取list集合的元素个数
        System.out.println(list.size());
        //list.clear();
        //System.out.println(list.size());

        //
        //list.remove(1); //删除索引下标为1的元素
        //list.remove(0);
        //list.remove(Integer.valueOf(1));
        list.remove(Integer.valueOf(1000));
        System.out.println(list);
        System.out.println(list.get(1));

    }
}
